package com.ruoyi.demo.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.demo.domain.ReportDeviceData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.demo.mapper.ReportDeviceDataMapper;
import com.ruoyi.demo.service.IReportDeviceDataService;

/**
 * 设备数据Service业务层处理
 * 
 * @author shc
 * @date 2025-08-13
 */
@Service
public class ReportDeviceDataServiceImpl extends ServiceImpl<ReportDeviceDataMapper, ReportDeviceData> implements IReportDeviceDataService {
    @Autowired
    private ReportDeviceDataMapper reportDeviceDataMapper;
    
    /**
     * 查询设备数据列表
     * 
     * @param reportDeviceData 设备数据
     * @return 设备数据
     */
    @Override
    public List<ReportDeviceData> selectReportDeviceDataList(ReportDeviceData reportDeviceData) {
    	LambdaQueryWrapper<ReportDeviceData> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotNull(reportDeviceData.getTypeId())) {
			queryWrapper.eq(ReportDeviceData::getTypeId, reportDeviceData.getTypeId());
		}
        if (StringUtils.isNotEmpty(reportDeviceData.getGuid())) {
			queryWrapper.eq(ReportDeviceData::getGuid, reportDeviceData.getGuid());
		}
        if (StringUtils.isNotNull(reportDeviceData.getBatchId())) {
			queryWrapper.eq(ReportDeviceData::getBatchId, reportDeviceData.getBatchId());
		}
        if (StringUtils.isNotEmpty(reportDeviceData.getTaskCode())) {
			queryWrapper.eq(ReportDeviceData::getTaskCode, reportDeviceData.getTaskCode());
		}
        if (StringUtils.isNotEmpty(reportDeviceData.getStation())) {
			queryWrapper.eq(ReportDeviceData::getStation, reportDeviceData.getStation());
		}
        if (StringUtils.isNotEmpty(reportDeviceData.getLabel())) {
			queryWrapper.eq(ReportDeviceData::getLabel, reportDeviceData.getLabel());
		}
        if (StringUtils.isNotEmpty(reportDeviceData.getKw())) {
			queryWrapper.eq(ReportDeviceData::getKw, reportDeviceData.getKw());
		}
        if (StringUtils.isNotEmpty(reportDeviceData.getValue())) {
			queryWrapper.eq(ReportDeviceData::getValue, reportDeviceData.getValue());
		}
        if (StringUtils.isNotNull(reportDeviceData.getValueType())) {
			queryWrapper.eq(ReportDeviceData::getValueType, reportDeviceData.getValueType());
		}
        if (StringUtils.isNotNull(reportDeviceData.getReportTime())) {
			queryWrapper.eq(ReportDeviceData::getReportTime, reportDeviceData.getReportTime());
		}
        if (StringUtils.isNotNull(reportDeviceData.getVersion())) {
			queryWrapper.eq(ReportDeviceData::getVersion, reportDeviceData.getVersion());
		}
        return reportDeviceDataMapper.selectList(queryWrapper);
    }

    @Override
    public int keepLatestReportPower(String sn) {

		// 1. 构造查询条件：查询指定用户的所有登录记录
		QueryWrapper<ReportDeviceData> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("task_code", sn).eq("station","老化后+M2调试").eq("kw","reportPower")
				.orderByDesc("create_time"); // 按登录时间降序排序

		// 2. 获取所有符合条件的记录
		List<ReportDeviceData> records = reportDeviceDataMapper.selectList(queryWrapper);

		// 3. 如果记录数小于等于1，无需删除
		if (records.size() <= 1) {
			throw new RuntimeException("sn"+sn+"只有一条最新数据，无需处理");
		}

		// 4. 保留第一条（最新的）记录，收集其余记录的ID
		List<Long> idsToDelete = records.stream()
				.skip(1) // 跳过第一条（最新的）
				.map(ReportDeviceData::getId)
				.collect(Collectors.toList());

		// 5. 批量删除其余记录
//		int deleteCount = baseMapper.deleteBatchIds(idsToDelete);
		if(StringUtils.isNull(idsToDelete)){
			throw new RuntimeException("sn"+sn+"没有可重复的数据");
		}else{
			return  baseMapper.deleteBatchIds(idsToDelete);
		}

    }
}
